package com.googlecode.bip.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.NamingException;
import com.googlecode.bip.dao.utils.Listable;
import com.googlecode.bip.entity.IUser;
import com.googlecode.bip.entity.User;

/**
 * Manage 'users' table - private implementation
 *
 * @author Claude CHOISNET
 */
public abstract class UserDAO
        implements Listable<IUser>
{
    private static final long serialVersionUID = 1L;

    /**
     * Create a new UserDAO
     */
    public UserDAO()
    {
    }

    @Override //Listable<IUser>
    public IUser newInstance(final ResultSet rSet )
        throws SQLException
    {
        return new User().set( rSet );
    }

    /**
     * Find user in database using username and password (login)
     *
     * @param username	login username
     * @param password	login password
     * @return a new IUser if user is found, null otherwise
     * @throws NamingException if any
     * @throws SQLException if any
     */
    public abstract IUser findUser(
            final String username,
            final String password
            )
        throws NamingException, SQLException;

    /**
     * Find user in database using iduser
     *
     * @param iduser iduser to find
     * @return a new IUser if user is found, null otherwise
     * @throws NamingException if any
     * @throws SQLException if any
     */
    public abstract IUser findUser(
            final int iduser
            )
        throws NamingException, SQLException;

    /**
     * Insert a new User (must not exist)
      * <br/>
     * {@link IUser#getIdUser()} value is ignored by insertion (must be equal 0)
     * <br/>
     * After database insert this method update {@link IUser#getIdUser()} content
     * with the index created by database.
     * <p>
     * This method set ONLY values from : {@link IUser#getUserName()},
     * and password. Other values are ignored.
     * </p>
     *
     * @param user User to insert
     * @return true if User has been created
     */
    public abstract boolean insertUser( final IUser iuser );

    /**
     * Delete a User
     *
     * @param iduser iduser to find
     * @return true if User has been deleted, false on failure
     * @throws NamingException if any
     * @throws SQLException if any
     */
    public abstract boolean deleteUser( final int iduser )
        throws NamingException, SQLException;

    /**
     * Update User in database
     *
     * @param user user to update
     * @return true on success, false on failure or error
     * @throws NamingException if any
     * @throws SQLException if any
     */
    public abstract boolean updateUser( final IUser iuser )
        throws NamingException, SQLException;

}

